#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>



int main(int agrc,char** argv)
{
    int sock = socket(AF_INET, SOCK_STREAM, 0);

    if(-1 == sock)
    {
        perror("socket");
        return 1;
    }

    struct sockaddr_in srv_addr;
    srv_addr.sin_family = AF_INET;
    srv_addr.sin_addr.s_addr = inet_addr(argv[1]);
    srv_addr.sin_port = htons(atoi(argv[2]));

    if(-1 == connect(sock, (struct sockaddr*)&srv_addr, sizeof(srv_addr)))
    {
        perror("connect");
        return 1;
    }
    
    char msg[300] = "";
    int ret;
    ret= recv(sock,msg,sizeof(msg), 0);

    int fd = open(msg,O_WRONLY | O_CREAT,0666);
    
    while(1)
    {
        ret = recv(sock, msg, sizeof(msg), 0);
        if(ret <= 0) 
        {
        	break;
        }
        else
        {
        	write(fd,msg,ret);
    	}
    }

    close(fd);
    // 第五步：端开连接
    close(sock);
    
    return 0;
}




